<?php 
use Wdl\helper\webHelper;

defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
load()->func('tpl'); 
$uniacid = webHelper::bindUniacid(); 
$openid = $_W['openid'];
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';	
$template_name = sprintf("option/%s",  $operation);

$rd = !empty($_GPC['rd']) ? $_GPC['rd'] : 'index';	

if($operation =='btbar' || $operation =='navmenus'){
    if($operation =='btbar')
        $cd_type = 0; //底部导航
    else    
        $cd_type = 1; //首页
        //菜单新增或者编辑的处理
    if($rd == 'add'){
        if (checksubmit('submit')) {
            $rs = webHelper::fourm_post( $cd_type);
            if($rs['code']) 
                message($rs['msg'], "", 'error');
            else
                message("操作成功", webHelper::WebUrl('option',['op'=> $operation]), 'success'); 
        }
        if(!empty($_GPC['id'])){
            $id = intval($_GPC['id']);
            $category = pdo_fetch("SELECT * FROM ".tablename('kbwdl_forums')." WHERE id = '$id' AND uniacid = '{$uniacid}' and type={$cd_type}");
            
        }
    }elseif($rd=='change_status'){

        $rs = webHelper::fourm_change_status( $cd_type);
        if($rs) 
            iajax(0, '更改成功！', $this->createWebUrl('option', array('op' => $operation)));
        else  
            iajax(1, '更改失败！', '');
    }elseif($rd=='delete'){       
        if (checksubmit('submit')) {
            foreach ($_GPC['rid'] as $key => $id) {                
                webHelper::fourm_delete(intval($id));
            }
            itoast('批量删除成功！', $this->createWebUrl('option', array('op' => $operation)), 'success');
        } else {
            $id = intval($_GPC['id']);
            webHelper::fourm_delete(intval($id)); 
            itoast('删除成功！', $this->createWebUrl('option', array('op' => $operation)), 'success');
        }
    }else{
        $children = array();
        $category = pdo_fetchall("SELECT * FROM ".tablename('kbwdl_forums')." WHERE uniacid = {$uniacid} and type={$cd_type} ORDER BY orderid ASC");
        foreach ($category as $index => $row) {
            if (!empty($row['parentid'])){
                $children[$row['parentid']][] = $row;
                unset($category[$index]);
            }
        }
    }
    $open_target = ['本小程序内部跳转','跳转关联公众号网址','跳转关联小程序'];
    $siteLinks = [
            0=> array(
                'name'=> '新房',
                'pathurl'=> '/pages/house/house?zhutype=3',
                'icon'=>'icon-dalou',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),
            1=>  array(
                'name'=> '二手房',
                'pathurl'=> '/pages/house/house',
                'icon'=>'icon-jianyuede',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),
            2=>  array(
                'name'=> '找租房',
                'pathurl'=> '/pages/house/house?zhutype=2',
                'icon'=>'icon-zufang',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),
            3=>  array(
                'name'=> '找商铺',
                'pathurl'=> '/pages/house/house?zhutype=4',
                'icon'=>'icon-shop-full',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),
            4=>  array(
                'name'=> '写字楼',
                'pathurl'=> '/pages/house/house?zhutype=9',
                'icon'=>'icon-xiezilou',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),
            5=>  array(
                'name'=> '仓库',
                'pathurl'=> '/pages/house/house?zhutype=8',
                'icon'=>'icon-zhusu',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),
            6=>  array(
                'name'=> '我要出租',
                'pathurl'=> '/pages/send/sell?type=chuzu',
                'icon'=>'icon-yijianfankui',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),

            7=>  array(
                'name'=> '我要卖房',
                'pathurl'=> '/pages/send/sell?type=chushou',
                'icon'=>'icon-ziyouanpai',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),

            8=>  array(
                'name'=> '求购',
                'pathurl'=> '/pages/send/index?type=qiugou',
                'icon'=>'icon-jingjide',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),

            9=>  array(
                'name'=> '求租',
                'pathurl'=> '/pages/send/index?type=qiuzu',
                'icon'=>'icon-shushidu',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),
            10=>  array(
                'name'=> '厂房',
                'pathurl'=> '/pages/house/house?zhutype=7',
                'icon'=>'icon-gaoduande',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),
            11=>  array(
                'name'=> '地图找房',
                'pathurl'=> '/pages/map/map',
                'icon'=>'icon-zuobiao',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),
            12=>  array(
                'name'=> '贷款计算',
                'pathurl'=> '/pages/tools/tools',
                'icon'=>'icon-icon--',
                'iconpic'=>'',
                'isshow'=>1,
                'order'=>0,
            ),
        ];

    //两种操作用同一个模板
    $template_name = sprintf("option/%s",   'fourm');

    //房源参数
}elseif($operation == 'sechouse'){
    if (checksubmit('submit')) {
        $data = $_GPC['pay'];

        $r = webHelper::update_place_data('formdata', $operation, $data );

        message("编辑成功", webHelper::WebUrl('option',['op'=> $operation]), 'success');
    }
    $settings = webHelper::fetch_place_data('formdata', $operation);
    //获取默认配置
    $adapter =  include  IA_ROOT. "/addons/kbwdl_shequzufang/vender/adapter.php";
    $settings_fields = webHelper::sechouse_fields();
    foreach($adapter['sechouse'] as $field => $val){
        if(!in_array($field , ['louceng','jingyin','charges_step'])){
            
             $field_value =  webHelper::str_to_array( $val);
             if(empty($settings[$field] ))
                $settings[$field] = implode(",", array_keys($field_value ));
        }
    }
    //楼层显示
    if(empty($settings['louceng'])){
        $settings['louceng'] = $adapter['sechouse']['louceng'];
    }
    //商铺业态
    if(empty($settings['jingyin'])){
        $jingyin = $adapter['sechouse']['jingyin'];
        //他是二维数组由hangye 和jingyin 组合成
        $tmp = [];
        $field_value =  webHelper::str_to_array($adapter['sechouse']['hangye'] );
        foreach($field_value as $val){
            $tmp[] =  sprintf("%s:%s", $val, isset($jingyin[$val])? implode(",",  $jingyin[$val] ) :"" );
        }
         $settings['jingyin'] = implode("\n", $tmp);
        unset($tmp, $val);
    }
    //其他用pay做参数的配置
}else{
 
    if (checksubmit('submit')) {
        $data = $_GPC['pay'];

        $r = webHelper::update_place_data('setting',$operation , $data );

        message("编辑成功", webHelper::WebUrl('option', ['op'=> $operation]), 'success');
    }
    $settings = webHelper::fetch_place_data('setting',$operation);
    
}
$submenus = [
    ['name'=> '参数设置', 'op'=> 'display'],
    ['name'=> '房源参数', 'op'=> 'sechouse'],
    ['name'=> '关于我们', 'op'=> 'company'],
    ['name'=> '小程序设置', 'op'=> 'wxapp'],
    ['name'=> '上架审核', 'op'=> 'security'],
   // ['name'=> '底部导航', 'op'=> 'btbar'],
    ['name'=> '首页导航', 'op'=> 'navmenus'],
   
];

include $this->template('option/layout');
